package org.jeecg.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.dto.UserGroupDto;
import org.jeecg.modules.business.dto.UserLabelDto;
import org.jeecg.modules.business.entity.*;
import org.jeecg.modules.business.mapper.*;
import org.jeecg.modules.business.service.IBusMarketMemberService;
import org.jeecg.modules.business.service.IBusMemberCardService;
import org.jeecg.modules.business.vo.MemberCardStatVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: bus_member_card
 * @Author: jeecg-boot
 * @Date:   2023-03-10
 * @Version: V1.0
 */
@Service
public class BusMemberCardServiceImpl extends ServiceImpl<BusMemberCardMapper, BusMemberCard> implements IBusMemberCardService {
    @Resource
    private BusMarketMemberGroupMapper busMarketMemberGroupMapper;
    @Resource
    private BusMarketMemberLableMapper busMarketMemberLableMapper;

    @Resource
    private IBusMarketMemberService marketMemberService;
    /**
     * 设置用户分组
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result groupBatch(UserGroupDto dto) {
        if (ObjectUtils.isEmpty(dto.getGroupId())) return Result.error("分组id不能为空");
        if (dto.getIds() == null || dto.getIds().size() <= 0) return Result.error("会员不能为空!");
        BusMarketMemberGroup group = busMarketMemberGroupMapper.selectById(dto.getGroupId());
        if (ObjectUtils.isEmpty(group)) {
            return Result.error("分组不存在");
        }
        dto.getIds().forEach(item -> {
            LambdaUpdateWrapper<BusMemberCard> updateWrapper = new UpdateWrapper().lambda();
            updateWrapper.set(BusMemberCard::getGroupId, dto.getGroupId());
            updateWrapper.eq(BusMemberCard::getId, item);
            this.update(updateWrapper);
        });
        return Result.ok("分组成功");
    }

    /**
     * 设置用户分组
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result lableBatch(UserLabelDto dto) {
        if (ObjectUtils.isEmpty(dto.getLabelId())) return Result.error("标签id不能为空");
        if (dto.getIds() == null || dto.getIds().size() <= 0) return Result.error("会员不能为空!");
        BusMarketMemberLable lable = busMarketMemberLableMapper.selectById(dto.getLabelId());
        if (ObjectUtils.isEmpty(lable)) {
            return Result.error("标签不存在");
        }
        dto.getIds().forEach(item -> {
            LambdaUpdateWrapper<BusMemberCard> updateWrapper = new UpdateWrapper().lambda();
            updateWrapper.set(BusMemberCard::getLabelId, dto.getLabelId());
            updateWrapper.eq(BusMemberCard::getId, item);
            this.update(updateWrapper);
        });
        return Result.ok("标签成功");
    }

    /**
     * 统计
     * @return
     */
    public MemberCardStatVo stat(String hotelId) {
        return baseMapper.stat(hotelId);
    }

    /**
     *
     * @param busMemberCard
     * @param money
     * @param feeId
     * @param hotelId
     * @return
     */
    public BusMemberBalanceLog cardPayFee(BusMemberCard busMemberCard, BigDecimal money, String feeId, String hotelId){
        LoginUser user = TokenUtils.getAuthUser();
        BigDecimal balance = busMemberCard.getBalance().subtract(money);
        if (balance.longValue() < 0){
            throw new JeecgBootException("会员卡余额不足");
        }
        busMemberCard.setBalance(balance);

        BusMemberBalanceLog balanceLog = new BusMemberBalanceLog();
        balanceLog.setOrderFeeId(feeId);
        balanceLog.setBalance(balance);
        balanceLog.setType(2);// 扣费
        balanceLog.setMemberId(busMemberCard.getId());
        balanceLog.setStatus(1);// TODO：不知道干什么用的 貌似是个啥支付状态
        balanceLog.setGiveMoney(new BigDecimal(0));
        balanceLog.setRemarks("收费扣款");
        balanceLog.setHotelId(hotelId);
        if(user!=null) {
            balanceLog.setTenantId(user.getRelTenantIds());
        }
        balanceLog.setPaymentMethod("");
        balanceLog.setPayMoney(new BigDecimal(0));
        balanceLog.setCode(BusMemberBalanceLogServiceImpl.randomNumber("KF"));
        balanceLog.setCreateTime(new Date());
        return balanceLog;
    }

    @Override
    public BusMarketMember getMarkerMemberByCarId(String id) {
        BusMemberCard memberCard = getById(id);
        if (memberCard == null){
            throw new JeecgBootException("未找到会员");
        }
        BusMarketMember marketMember = marketMemberService.getById(memberCard.getGradeId());
        if (marketMember == null){
            throw new JeecgBootException("未找到会员等级");
        }
        return marketMember;
    }
}
